package test.fun;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;

import java.io.IOException;
import java.util.ArrayList;

import static test.fun.GetConnet.connection;

/**
 * ClassName: GetFun
 * Package: test.fun
 * Description:
 *              Get方式查询数据
 * @Author ChenJun
 * @Create 2023/4/23 9:31
 * @Version 1.0
 */
public class GetFun {

    //TODO Get方式查询数据
    public static void GetDate(String tableName, String rowKey, String cf, String column) throws IOException {

        //获取表名
        Table table = connection.getTable(TableName.valueOf(tableName));

        //创建get对象
        Get get = new Get(rowKey.getBytes());

        //执行到cf
//        get.addFamily(cf.getBytes());

        //指定到列
//        get.addColumn(cf.getBytes(), column.getBytes());

        //执行get操作
//        table.get(get);

        Get get1 = new Get("1002".getBytes());
        ArrayList<Get> gets = new ArrayList<>();
        gets.add(get);
        gets.add(get1);

        Result[] results = table.get(gets);

        for (Result result : results) {
            //解析result
            Cell[] cells = result.rawCells();
            for (Cell cell : cells) {
                System.out.println("RK:" + new String(CellUtil.cloneRow(cell)) +
                        ",CF:" + new String(CellUtil.cloneFamily(cell)) +
                        ",CN:" + new String(CellUtil.cloneQualifier(cell)) +
                        ",Value:" + new String(CellUtil.cloneValue(cell)));
            }
        }

        //关闭Table对象
        table.close();
    }
}
